const userName = document.querySelector('#userName');
const pwd = document.querySelector('#pwd');
const loginButton = document.querySelector('#login-button');
loginButton.onclick = () => {
  methods.login();
}
const methods = {
  login() {
    if (!userName.value || !pwd.value) {
      return;
    }
    const data = {
      username: userName.value.trim(),
      password: pwd.value.trim()
    }
    fetch('https://www.linyannova.top/api/auth/login', {
      method: 'POST',
      headers: {
          'Content-Type': 'application/json'
      },
      body: JSON.stringify(data)
    }).then(response => {
      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
      }
      return response.json();
    }).then((res) => {
      if (res.code === 0) {
        sessionStorage.setItem('user', JSON.stringify(res.data));
        location.href = '../map/index.html'
      }
    })
  }
}
